<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" title="职务选择列表" width="800px">
    <DataGridTable
      v-loading="loading"
      :sup_this="sup_this"
      :data="data.rtnList"
      :multi-select="false"
      :show-row-number="true"
      :show-page="false"
      table-ref="jobTable"
      @current-change="handleCurrentChange">
      <el-table-column prop="jobName" label="职位名称">
        <template slot="header" slot-scope="scope">
          <el-input v-model="jobName" clearable placeholder="输入回车过滤" style="width: 100%;" class="filter-item" @keyup.enter.native="filterQuery"/>
        </template>
      </el-table-column>
      <el-table-column prop="jobCode" label="系统编码" />
      <el-table-column label="职位类别" align="center">
        <template slot-scope="scope">
          <div v-for="item in dicts.job_type" :key="item.id">
            <el-tag v-if="scope.row.jobType.toString() === item.dictCode" :type="scope.row.jobType ? '' : 'info'">{{ item.dictName }}</el-tag>
          </div>
        </template>
      </el-table-column>
    </DataGridTable>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import initData from '@/mixins/initData'
import { findJobByCpyCode } from '@/api/system/sysJob'
import { parseTime } from '@/utils/index'
import { getUserInfo } from '@/utils/auth'

export default {
  mixins: [initData],
  dictTypes: ['job_type'],
  props: {
    mult_select: {
      default: true,
      requried: false,
      type: Boolean
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      selectedRows: [],
      selectedRow: {},
      sup_this: this,
      allList: [],
      jobName: ''
    }
  },
  created() {
    this.url = findJobByCpyCode()
    this.pageCount = false
  },
  methods: {
    parseTime,
    async beforeInit() {
      this.selectedRows = []
      this.selectedRow = {}
      this.paramBean.cpyCode = getUserInfo().cpyCode
      return true
    },
    afterInit(res) {
      this.allList = res.data.rtnList
    },
    filterQuery() {
      const jobName = this.jobName
      if (jobName) {
        this.data.rtnList = this.allList.filter(row => {
          return row.jobName.includes(jobName)
        })
      } else {
        this.data.rtnList = this.allList
      }
    },
    handleCurrentChange(val) {
      this.selectedRow = val
    },
    handleSelectionChange(val) {
      this.selectedRows = val
    },
    doSubmit() {
      this.loading = true
      if (this.mult_select) {
        this.$emit('jobSelectCallback', this.selectedRows, this)
      } else {
        this.$emit('jobSelectCallback', this.selectedRow, this)
      }
    },
    cancel() {
      this.dialog = false
    }
  }
}
</script>
